//index.js
const utils = require("../../utils/utils.js");
const biz = require("../../utils/bizUtils.js");
const listUtil = require("../../utils/listUtil.js");
const session = require("../../utils/session.js");
const toast = require("../../utils/toasts.js");
const wxm = require("../../utils/wechat_modules.js");
const com = require("../../utils/commonRequest.js");
const cache = require("../../utils/cache.js");

import {
  coupon_receive
} from "../../utils/api.js";

//获取应用实例
const app = getApp();
const globalData = app.globalData;

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    list: [],
    hidden: true,
    scrollTop: 0,
    scrollHeight: 0,
    title: '',
    keyword: null,
    catId: null,
    orderRule: 'HOT',
    waiting: true,
    share_modal: false,
    noMore: false,
    isIphoneX: globalData.isIphoneX,
    isFirst: true
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function(options) {
    var that = this;
    toast.showLoading();
    wx.getSystemInfo({
      success: function(res) {
        console.info(res.windowHeight);
        that.setData({
          scrollHeight: res.windowHeight
        });
      }
    });
    this.getUserInfo();
    if (options) {

      if (options.cardId) {
        this.setData({
          cardId: options.cardId
        })

        var that = this;
        var user = session.getUserSync();
        this.setData({
          isMy: (user.id == options.cardId)
        });
      }

      if (options.sellerId) {
        this.setData({
          sellerId: options.sellerId
        })

        var that = this;
        var user = session.getUserSync();
        this.setData({
          isMy: (user.sellerId == options.sellerId)
        });
      }
    }
  },
  onShow: function() {
    if (this.data.isFirst || this.data.isEdit) {
      this.setData({
        isFirst: false
      });
      this.refresh();
    }
  },
  // 获取当前用户信息
  getUserInfo: function() {
    session.getUser(this);
  },
  onShareAppMessage: function(res) {
    return wxm.commonCardShare(this, this.data.user);
  },
  getList() {
    console.log("---" + this.data.location);
    var queryParam = {};
    var type = this.data.type;
    queryParam.orderRule = this.data.orderRule;

    var api = "/bargain/query";
    queryParam.isMy = true;


    if (this.data.sellerId) {
      queryParam.sellerId = this.data.sellerId;
    }
    var that = this;

    listUtil.getList(this, api, queryParam, function(item) {
      item.endTimeStr = utils.tf('yyyy-MM-dd', item.activityEndTime);
      item.expiredTimeStr = utils.tf('yyyy-MM-dd hh:mm:ss', item.expiredTime);
      if (item.cover) {
        item.cover1 = item.cover.split(',')[0];
      }

      item.alcutprice = utils.moneyStyle(item.originalPrice - item.currentPrice);
      item.percent = (item.originalPrice - item.currentPrice) * 100 / (item.originalPrice-item.minPrice);

    });
  },
  bindDownLoad: function() {
    getList();
  },
  scroll: function(event) {
    listUtil.scroll(this, event);
  },
  refresh: function(event) {
    listUtil.refresh(this);
  },
  onPullDownRefresh: function() {
    listUtil.onPullDownRefresh(this);
  },
  onReachBottom: function() {
    listUtil.onReachBottom(this);
  },
  receiveCoupon: function(e) {
    var index = e.currentTarget.dataset.index;
    var list = this.data.list;
    var item = list[index];
    var id = item.id;

    if (this.data.isEdit) {
      wx.navigateTo({
        url: '/pages/couponAdd/couponAdd?id=' + id,
      })
      return;
    }

    if (!item.canGet) {
      return;
    }

    if (item.status == 'EXPIRED') {
      return;
    }

    if (item.status == 'NOTSTART') {
      return toast.show('尚未开始');
    }

    if (this.data.isMy) {
      toast.show('无法领取自己的优惠券');
      return;
    }

    toast.showLoading();
    var that = this;
    coupon_receive({
      id: id
    }).then(res => {
      if (res.success) {
        item.remainCount = item.remainCount - 1;
        list[index] = item;
        this.setData({
          list: list
        });
        toast.show('领取成功');
      } else {
        toast.show(res.errorMsg);
      }
    });
  },
  gotoAdd: function() {
    if (this.data.isEdit) {
      wx.navigateTo({
        url: '/pages/cutAdd/cutAdd',
      })
    } else {
      wx.navigateTo({
        url: '/pages/couponList/couponList?cardId=' + this.data.user.id + '&isEdit=true',
      })
    }
  },
  pushFormSubmit: function(e) {
    console.log(e);
    console.log("formId:" + e.detail.formId);
    com.saveFormId(e);
  }
})